<template>
  <div class="container">
    <!-- 左边的 el-card -->
    <el-card class="left-card">
            <template #header>
        <svg-icon icon-class="menu" />
        一级分类
                      <el-button
          type="text"
          icon="plus"
          @click="showAddTopLevelCategoryDialog"
          >新增一级分类</el-button
        >
      </template>

      <!-- <div slot="header">
        <span>一级分类</span>
        <el-button
          type="text"
          icon="plus"
          @click="showAddTopLevelCategoryDialog"
          >新增一级分类</el-button
        >
      </div> -->
      <div class="top-level-categories">
        <div
          v-for="category in topLevelCategories"
          :key="category.genreId"
          class="category"
        >
          <span @click="showCategoryDetail(category.genreId)">{{
            category.name
          }}</span>
          <el-button
            type="text"
            icon="edit"
            @click="editCategory(category)"
            >修改</el-button
          >
          <el-button
          v-if="category.status === 'NORMAL'"
            type="text"
            icon="close"
            @click="disableCategory(category.genreId)"
            >停用</el-button
          >
                    <el-button
          v-if="category.status === 'CANCEL'"
            type="text"
            icon="close"
            @click="enableCategory(category.genreId)"
            >启用</el-button
          >
          <el-button
            type="text"
            icon="delete"
            @click="deleteCategory(category.genreId)"
            >删除</el-button
          >
        </div>
      </div>
    </el-card>

    <!-- 右边的 el-card -->
    <el-card class="right-card">
      <template #header>
        <svg-icon icon-class="menu" />
        品类信息
                              <el-button
                      
          type="text"
          icon="plus"
          @click="showAddTopLevelCategoryDialog"
          ></el-button
        >
      </template>
      <div class="category-detail">
        <!-- <h3>{{ selectedCategory.name }} </h3> -->
        <!-- 这里显示一级分类的详细信息 -->

        <el-form
          ref="form"
          :model="selectedCategory"
          :rules="rules"
          label-width="100px"
        >
          <el-row>
            <el-col :span="5">
              <el-form-item label="品类名称" prop="name">
                <el-input v-model="selectedCategory.name" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="品类状态" prop="status">
                <el-select v-model="selectedCategory.status" disabled>
                  <el-option
                    v-for="dict in product_pattern_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="显示顺序" prop="num">
                <el-input v-model="selectedCategory.num" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="创建时间" prop="createTime">
                <el-input v-model="selectedCategory.createTime" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="更新时间" prop="updateTime">
                <el-input v-model="selectedCategory.updateTime" disabled />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <br />
      <br />
      <br />
      <svg-icon icon-class="menu" />
      二级品类列表
      <el-button @click="showAddTwoLevelCategoryDialog" type="text">
        添加二级品类</el-button
      >
      <hr class="custom-hr" />
      <div class="sub-categories">
         <el-row>
            <div class="item" v-for="category in subCategories" :key="category.id">
              <el-col :span="24">
                <div>{{ category.name }}&nbsp;</div>

    <el-button
            type="text"
            icon="edit"
            @click="editSubCategory(category)"
            >修改</el-button
          >
          <el-button
            v-if="category.status === 'NORMAL'"
            type="text"
            icon="close"
            @click="disableSubCategory(category.genreId)"
            >停用</el-button
          >
          <el-button
            v-if="category.status === 'CANCEL'"
            type="text"
            icon="close"
            @click="enableSubCategory(category.genreId)"
            >启用</el-button
          >
          <el-button
            type="text"
            icon="delete"
            @click="deleteSubCategory(category.genreId)"
            >删除</el-button
          >
              </el-col>
            </div>
          </el-row>
        <!-- <div
          v-for="category in subCategories"
          :key="category.id"
          class="category"
        >
          <span>{{ category.name }}</span>
          <el-button
            type="text"
            icon="edit"
            @click="editSubCategory(category)"
            >修改</el-button
          >
          <el-button
            v-if="category.status === 'NORMAL'"
            type="text"
            icon="close"
            @click="disableSubCategory(category.genreId)"
            >停用</el-button
          >
          <el-button
            v-if="category.status === 'CANCEL'"
            type="text"
            icon="close"
            @click="enableSubCategory(category.genreId)"
            >启用</el-button
          >
          <el-button
            type="text"
            icon="delete"
            @click="deleteSubCategory(category.genreId)"
            >删除</el-button
          >
        </div> -->
      </div>
    </el-card>

    <!-- 新增一级分类对话框 -->
    <el-dialog
      v-model="open"
      title="新增/修改品类"
      width="500px"
      @close="handleDialogClose"
    >
      <el-form :model="formData" ref="form" :rules="rules" label-width="100px">
        <el-form-item label="品类名称" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="品类状态" prop="status">
          <el-select v-model="formData.status">
            <el-option label="启用" value="NORMAL"></el-option>
            <el-option label="停用" value="CANCEL"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示顺序" prop="num">
          <el-input v-model="formData.num" type="number"></el-input>
        </el-form-item>
        <el-form-item label="品类说明" prop="memo">
          <el-input v-model="formData.memo"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="handleDialogClose">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, computed } from "vue";
import {
  loadOneProductGenreList,
  loadTwoProductGenreList,
  getGenre,
  delGenre,
  addGenre,
  updateGenre,
} from "@/api/order/genre.js";
import { getCurrentInstance } from "vue";
export default {
  setup() {
    // 获取当前组件实例
    const instance = getCurrentInstance();
    const { product_pattern_status } = instance.proxy.useDict(
      "product_pattern_status"
    );
    return {
      product_pattern_status,
    };
  },
  name: "Genre",
  data() {
    return {
      categoryParentId: null,
      // 遮罩层
      loading: true,
      isDetail: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        genreId: null,
        companyId: null,
        name: null,
        parentId: null,
        status: null,
        genrePath: null,
        level: null,
        floor: null,
        num: null,
        memo: null,
        imagePath: null,
        createTime: null,
        createEmployeeId: null,
        updateTime: null,
        updateEmployeeId: null,
      },
      formData: {
        genreId: null,
        parentId: null,
        name: "",
        status: "NORMAL",
        floor: "",
        memo: "",
      },
      rules: {
        name: [{ required: true, message: "请输入品类名称", trigger: "blur" }],
        status: [
          { required: true, message: "请选择品类状态", trigger: "change" },
        ],
        order: [{ required: true, message: "请输入显示顺序", trigger: "blur" }],
      },
      topLevelCategories: [],
      selectedCategory: {},
      subCategories: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      loadOneProductGenreList(this.queryParams).then((response) => {
        this.topLevelCategories = response;
        this.loading = false;
      });
    },

    // 打开新增一级分类弹出框
    showAddTopLevelCategoryDialog() {
      this.formData = {
        parentId: null,
        genreId: null,
        name: "",
        status: "NORMAL",
        floor: "",
        memo: "",
      };
      this.open = true;
    },
    // 打开新增二级分类弹出框
    showAddTwoLevelCategoryDialog() {
      if (this.categoryParentId == null) {
        alert("请先选择一级分类.");
        return;
      }
      this.open = true;
      this.formData = {
        genreId: null,
        parentId: null,
        name: "",
        status: "NORMAL",
        floor: "",
        memo: "",
      };
      this.formData.parentId = this.categoryParentId;
    },
    //关闭弹出框
    handleDialogClose() {
      this.open = false;
      this.formData.parentId = null;
    },
    //点击确定按钮提交
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.formData.genreId != null) {
            updateGenre(this.formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              
              if (this.formData.parentId != null) {
                loadTwoProductGenreList(this.formData.parentId).then(
                  (response) => {
                    this.subCategories = response;
                  }
                );
              }
            });
          } else {
            addGenre(this.formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              if (this.formData.parentId != null) {
                loadTwoProductGenreList(this.formData.parentId).then(
                  (response) => {
                    this.subCategories = response;
                  }
                );
              }
            });
          }
        }
      });
    },

    //点击一级分类名称,带出二级信息
    showCategoryDetail(categoryId) {
      this.categoryParentId = categoryId;
      loadTwoProductGenreList(categoryId).then((response) => {
        this.subCategories = response;
        this.loading = false;
      });
      this.selectedCategory = this.topLevelCategories.find(
        (category) => category.genreId === categoryId
      );
    },

    // 编辑一级分类
    editCategory(category) {
      console.log("编辑一级分类:", category.categoryId);
      this.formData = { ...category };
      this.open = true;
    },
    // 停用一级分类
    disableCategory(categoryId) {

       const updateForm = {
        genreId: categoryId,
        status: "CANCEL",
      };
      updateGenre(updateForm).then((response) => {
        this.$modal.msgSuccess("停用成功");
        this.getList();
      });
    },
     // 启用一级分类
        enableCategory(categoryId) {

             const updateForm = {
        genreId: categoryId,
        status: "NORMAL",
      };
      updateGenre(updateForm).then((response) => {
        this.$modal.msgSuccess("启用成功");
        this.getList();
      });
    },
    
    /** 删除一级分类 */
    deleteCategory(categoryId) {
      this.$modal
        .confirm('是否确认删除一级品类编号为"' + categoryId + '"的数据项？')
        .then(function () {
          return delGenre(categoryId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    // 编辑二级分类
    editSubCategory(category) {
      console.log("编辑二级分类:", category.categoryId);
      this.formData = { ...category };
      this.formData.parentId = this.categoryParentId;
      this.open = true;
    },
    // 停用二级分类
    disableSubCategory(categoryId) {
      const updateForm = {
        genreId: categoryId,
        status: "CANCEL",
      };
      updateGenre(updateForm).then((response) => {
        this.$modal.msgSuccess("停用成功");
        this.getList();
        if (this.categoryParentId != null) {
          loadTwoProductGenreList(this.categoryParentId).then((response) => {
            this.subCategories = response;
          });
        }
      });
    },
    // 启用二级分类
    enableSubCategory(categoryId) {
      console.log("启用二级分类:", categoryId);
      const updateForm = {
        genreId: categoryId,
        status: "NORMAL",
      };
      updateGenre(updateForm).then((response) => {
        this.$modal.msgSuccess("启用成功");
        this.getList();
        if (this.categoryParentId != null) {
          loadTwoProductGenreList(this.categoryParentId).then((response) => {
            this.subCategories = response;
          });
        }
      });
    },

    // 删除二级分类
    deleteSubCategory(categoryId) {
      this.$modal
        .confirm('是否确认删除二级品类编号为"' + categoryId + '"的数据项？')
        .then(function () {
          return delGenre(categoryId);
        })
        .then(() => {
          this.getList();
          loadTwoProductGenreList(this.categoryParentId).then((response) => {
            this.subCategories = response;
            this.loading = false;
          });
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.container {
    height: 100vh; /* 设置高度为浏览器窗口的高度 */
    display: flex;
}

.left-card {
  flex: 1;
}

.right-card {
  flex: 4;
}

.category {
  margin-bottom: 10px;
}

/* 自定义横线的样式 */
.custom-hr {
  border-color: #fbf7f7fb; /* 设置横线的颜色为红色 */
}
.item {
  width: calc(25% - 10px);
  margin-bottom: 10px;
  background-color: #f0f0f0;
  padding: 20px;
  margin-right: 10px;
  background-clip: padding-box
}
</style>
